package ${package}.rest;

import FileUtil;
import Log;
import PageResult;
import ${package}.entity.${className};
import ${package}.service.${className}Service;
import ${package}.dto.${className}QueryDTO;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ${author}
* @date ${date}
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "${apiAlias}管理")
@RequestMapping("/api/${changeClassName}")
public class ${className}Controller {

    private final ${className}Service ${changeClassName}Service;

    @ApiOperation("分页查询")
    @GetMapping
    @PreAuthorize("@ss.check('${changeClassName}:list')")
    public ResponseEntity<PageResult<${className}>> paging(${className}QueryDTO dto){
        return new ResponseEntity<>(${changeClassName}Service.paging(dto), HttpStatus.OK);
    }

    @Log(module = "${apiAlias}管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@ss.check('${changeClassName}:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody ${className} entity){
        ${changeClassName}Service.save(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log(module = "${apiAlias}管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@ss.check('${changeClassName}:edit')")
    public ResponseEntity<Object> modify(@Validated @RequestBody ${className} entity){
        ${changeClassName}Service.updateById(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(module = "${apiAlias}管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@ss.check('${changeClassName}:del')")
    public ResponseEntity<Object> remove(@RequestBody List<${pkColumnType}> ids) {
        ${changeClassName}Service.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('${changeClassName}:list')")
    public void export(HttpServletResponse response, ${className}QueryDTO dto) throws IOException {
        List<${className}> list = ${changeClassName}Service.list(dto);
        List<Map<String, Object>> result = new ArrayList<>();
        for (${className} ${changeClassName} : list) {
            Map<String, Object> map = new LinkedHashMap<>();
    <#list columns as column>
        <#if column.columnKey != 'PRI'>
            <#if column.remark != ''>
            map.put("${column.remark}", ${changeClassName}.get${column.capitalColumnName}());
            <#else>
            map.put(" ${column.changeColumnName}",  ${changeClassName}.get${column.capitalColumnName}());
            </#if>
        </#if>
    </#list>
            result.add(map);
        }
        FileUtil.downloadExcel(result, response);
    }

 }
